package com.baymax.test.scene.view

import android.animation.TypeEvaluator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Property
import android.widget.FrameLayout


/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.view
 * @class:       HightLightWrapper
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2023/3/13 14:52
 * @Copyright (C) 2023 YSTEN
 * @author:       xuyujian
 */
class HighLightWrapper @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {
    val DEFAULT_BACKGROUND_COLOR = -0x4e000000

    var f = RectF()
    val paint = Paint().apply {
        isAntiAlias = true
        xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
        //设置画笔遮罩滤镜,可以传入BlurMaskFilter或EmbossMaskFilter，前者为模糊遮罩滤镜而后者为浮雕遮罩滤镜
        //这个方法已经被标注为过时的方法了，如果你的应用启用了硬件加速，你是看不到任何阴影效果的
        //设置画笔遮罩滤镜,可以传入BlurMaskFilter或EmbossMaskFilter，前者为模糊遮罩滤镜而后者为浮雕遮罩滤镜
        //这个方法已经被标注为过时的方法了，如果你的应用启用了硬件加速，你是看不到任何阴影效果的
        // maskFilter = BlurMaskFilter(10f, BlurMaskFilter.Blur.INNER)
    }

    init {

        setLayerType(LAYER_TYPE_SOFTWARE, null);

        //ViewGroup默认设定为true，会使onDraw方法不执行，如果复写了onDraw(Canvas)方法，需要清除此标记
        setWillNotDraw(false)

    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawColor(DEFAULT_BACKGROUND_COLOR)
        drawHighlights(canvas)
    }

    private fun drawHighlights(canvas: Canvas) {
        canvas.drawRoundRect(f, 10f, 10f, paint)
    }


    fun setBounds(rect: RectF) {
        this.f = rect
    }




    val property by lazy { HighLightProperty(RectF::class.java ,"rectF") }


    class HighLightProperty(type: Class<RectF>, name: String) :
        Property<HighLightWrapper, RectF>(type, name) {
        override fun get(`object`: HighLightWrapper): RectF {
            return `object`.f
        }

        override operator fun set(
            `object`: HighLightWrapper,
            value: RectF
        ) {
            `object`.setBounds(value)
            `object`.postInvalidate()
        }
    }

    class RectFEvaluator(private val mRect: RectF) : TypeEvaluator<RectF> {
        override fun evaluate(fraction: Float, startValue: RectF, endValue: RectF): RectF {
            val left = startValue.left + (endValue.left - startValue.left) * fraction
            val top = startValue.top + (endValue.top - startValue.top) * fraction
            val right = startValue.right + (endValue.right - startValue.right) * fraction
            val bottom = startValue.bottom + (endValue.bottom - startValue.bottom) * fraction
            return run {
                mRect[left, top, right] = bottom
                mRect
            }
        }
    }
}